<!DOCTYPE HTML>
<html>
<head>
    <title> 搜索表单</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="csrf-token" content="{{ csrf_token() }}"/>
    <link rel="stylesheet" type="text/css" href="/assets/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="/assets/easyui/themes/icon.css" />
    <script type="text/javascript" src="/assets/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/assets/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/js/easyui_expand.js"></script>
</head>
<body>
<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'west',border:true,split:true," title="分类管理" style="width:150px; padding:5px;">
        <ul id="wu-category-tree" class="easyui-tree"></ul>
    </div>

    <div data-options="region:'center',border:false">
        <div id="toolbar">
            {!! $toolBarButton !!}
        </div>
        <table id="wu-datagrid"></table>

    </div>
    <input type="hidden" id="raw_id" name="raw_id" value="">
</div>

<!-- End of toolbar -->
<div id="form_dialog"></div>

<script>
    $(function(){

        var active_format = function (value) {
            return value ? '<img src="/css/icons/ok.png" alt="正常" title="正常" />' : '<img src="/css/icons/stop.png" alt="禁用" title="禁用" />';
        };
        var sex_format = function (value) {
            if (value === 'M') {
                return '男';
            }else{
                return '女';
            }
        };
        $('#wu-datagrid').datagrid({
            url: '/sys/user/list',
            rownumbers: true,
            toolbar: '#wu-toolbar',
            singleSelect:"true",
            queryParams:{
                _token : $('meta[name="csrf-token"]').attr('content'),
            },
            columns:[[
                { field:'id',checkbox:true},
                { field:'username',title:'用户名',width:150,align:'center'},
                { field:'realname',title:'姓名',width:100,align:'center'},
                { field:'sex',title:'性别',width:50,align:'center',formatter:sex_format},
                { field:'phone',title:'手机号',width:100,align:'center'},
                { field:'email',title:'Email',width:100,align:'center'},
                { field:'qq',title:'QQ',width:100,align:'center'},
                { field:'fax',title:'传真',width:100,align:'center'},
                { field:'remark',title:'备注',width:100,align:'center'},
                { field:'is_active',title:'有效',width:100,align:'center',formatter:active_format},
                { field:'is_admin',title:'管理员',width:100,align:'center',formatter:active_format}
            ]],
            onClickRow:function (index,raw) {
                $("input[name='raw_id']").val(raw.id);
            }
        });

    });

    function openAdd(){
        $('#form_dialog').dialog({
            title: '添加信息',
            width: 600,
            height: 350,
            closed: false,
            cache: false,
            href: '/sys/user/add',
            modal: true,
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: save
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#form_dialog').dialog('close');
                }
            }]
        });
    }

    $('#wu-category-tree').tree({
        url:'/sys/org/tree',
        queryParams:{
            _token : $('meta[name="csrf-token"]').attr('content')
        },
        onClick:function(node){
            $("input[name='org_id']").val(node.org_id);
            $('#wu-datagrid').datagrid({queryParams:{
                _token : $('meta[name="csrf-token"]').attr('content'),
                id: node.id
            }});
        }
    });

    function openEdit(){
        var raw_id = $("input[name='raw_id']").val();
        if (raw_id === '') {
            $.messager.alert('警告','请选择一行');
        }else{
            $('#form_dialog').dialog({
                title: '修改信息',
                width: 600,
                height: 350,
                closed: false,
                cache: false,
                href: '/sys/user/edit?id='+raw_id,
                modal: true,
                buttons: [{
                    text: '确定',
                    iconCls: 'icon-ok',
                    handler: save
                }, {
                    text: '取消',
                    iconCls: 'icon-cancel',
                    handler: function () {
                        $('#form_dialog').dialog('close');
                    }
                }]
            });
        }

    }

    function save(){

        $.ajax({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            url:'/sys/user/save',
            type:'post',
            data:$('#info-form').serialize(),
            dataType:'json',
            success:function (data) {
                if(data.code === 200){
                    $('#wu-datagrid').datagrid('reload',{_token : $('meta[name="csrf-token"]').attr('content')})
                    $('#form_dialog').dialog('close');
                }else{
                    $.messager.alert("提示信息", data.data , "error");
                }
            }
        });
    }

    /**
     * Name 删除记录
     */
    function remove(){
        var raw_id = $("input[name='raw_id']").val();
        $.messager.confirm('信息提示','确定要删除该记录？', function(result){
            if(result){

                $.ajax({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    type:'post',
                    url:'/sys/user/remove',
                    data:{
                        id:raw_id
                    },
                    success:function(data){
                        if(data.code === 200){
                            $.messager.alert('信息提示','删除成功！');
                            $('#wu-datagrid').datagrid('reload',{_token : $('meta[name="csrf-token"]').attr('content')});
                        }
                        else {
                            $.messager.alert('信息提示','删除失败,请稍后再试！');
                            $('#wu-datagrid').datagrid('reload',{_token : $('meta[name="csrf-token"]').attr('content')})
                        }
                    }
                });
            }
        });
    }

</script>
</body>
</html>
